/*******************************************************************************
 * Copyright (c) 2008, 2009 Fabio Mauro - Alberto Terraneo.
 * Permission is granted to copy, distribute and/or modify this document
 * under the terms of the GNU Free Documentation License, Version 1.2
 * or any later version published by the Free Software Foundation.
 *     
 * This program is distributed in the hope that it will be useful, 
 * but WITHOUT ANY WARRANTY; without even the implied 
 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
 * See the GNU Free Documentation License for more details.
 *     
 * You should have received a copy of the GNU Free Documentation 
 * License along with this distribution; if not, send a email to authors:
 * aterraneo <at> users.sourceforge.net
 * bigmoby09 <at> users.sourceforge.net
 *******************************************************************************/
package it.blueocean.acanto.web.platform.components.loginForm;

import org.apache.wicket.Application;
import org.apache.wicket.markup.html.form.CheckBox;
import org.apache.wicket.markup.html.form.FormComponent;
import org.apache.wicket.markup.html.form.PasswordTextField;
import org.apache.wicket.markup.html.form.StatelessForm;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.PropertyModel;
import org.apache.wicket.security.authentication.LoginException;
import org.apache.wicket.util.value.ValueMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Panel for user authentication.
 * 
 */
public abstract class AUsernamePasswordSignInPanel extends Panel
{
	private static final Logger log = LoggerFactory.getLogger(AUsernamePasswordSignInPanel.class);

	private static final long serialVersionUID = 955559933435803866L;

	/**
	 * Constructor.
	 * 
	 * @param id
	 *            component id
	 */
	public AUsernamePasswordSignInPanel(final String id)
	{
		super(id);
		add(new SignInForm("signInForm").setOutputMarkupId(false));		
	}

	/**
	 * The actual login process.
	 * 
	 * @param username
	 * @param password
	 * @return true, if the login was successful, false otherwise
	 * @throws LoginException
	 */
	public abstract void signIn(String username, String password) throws LoginException;

	/**
	 * Sign in form.
	 */
	public final class SignInForm extends StatelessForm<Object>
	{
		private static final long serialVersionUID = 1L;

		/**
		 * remember username
		 */
		private boolean rememberMe = true;

		/**
		 * Constructor.
		 * 
		 * @param id
		 *            id of the form component
		 */
		public SignInForm(final String id)
		{
			super(id, new CompoundPropertyModel<Object>(new ValueMap()));

			// only remember username, not passwords
			add(new TextField<String>("username").setPersistent(rememberMe).setOutputMarkupId(false));
			add(new PasswordTextField("password").setOutputMarkupId(false));
			add(new CheckBox("rememberMe", new PropertyModel<Boolean>(this, "rememberMe")));			
		}

		public String getMarkupId()
		{
			// fix javascript id
			return getId();
		}

		public final void onSubmit()
		{
			if (!rememberMe)
			{
				// delete persistent data
				getPage().removePersistedFormData(SignInForm.class, true);
			}

			ValueMap values = (ValueMap) getModelObject();
			String username = values.getString("username");
			String password = values.getString("password");

			try
			{
				signIn(username, password);

				// continue or homepage?
				if (!getPage().continueToOriginalDestination())
				{
					setResponsePage(Application.get().getHomePage());
				}

			}
			catch (LoginException e)
			{
				log.error(e.getMessage(), e);

				error(getLocalizer().getString("exception.login", this, e.getMessage()));
			}
		}

		/**
		 * @return true if formdata should be made persistent (cookie) for later logins.
		 * 
		 */
		public boolean getRememberMe()
		{
			return rememberMe;
		}

		/**
		 * Remember form values for later logins?.
		 * 
		 * @param rememberMe
		 *            true if formdata should be remembered
		 */
		public void setRememberMe(boolean rememberMe)
		{
			this.rememberMe = rememberMe;
			((FormComponent<?>) get("username")).setPersistent(rememberMe);
		}
	}
}
